<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Konva moveUp 切换节点层级</title>
  <script src="./konva.min.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    #container {
      background-color: #f0f0f0;
    }
  </style>
</head>

<body>
  <div id="container"></div>
  <button id="moveUpBtn">上移</button>

  <script>
    // 创建舞台
    const stage = new Konva.Stage({
      container: 'container',
      width: 500,
      height: 500,
    })

    // 创建层
    const layer = new Konva.Layer()
    stage.add(layer)

    // 创建矩形
    const rect1 = new Konva.Rect({
      x: 50,
      y: 50,
      width: 100,
      height: 100,
      fill: 'red',
    })

    const rect2 = new Konva.Rect({
      x: 70,
      y: 70,
      width: 100,
      height: 100,
      fill: 'blue',
    })

    // 添加矩形到层
    layer.add(rect1)
    layer.add(rect2)

    // 绘制层
    layer.draw()

    // 获取按钮并添加点击事件
    document.getElementById('moveUpBtn').addEventListener('click', () => {
      rect1.moveUp()
      layer.draw()
    });
  </script>
</body>

</html>